package cn.b.sky.mes.mm.service.impl;

import java.util.List;

import javax.annotation.Resource;

import cn.b.sky.mes.mm.dao.StorageLocationDao;
import cn.b.sky.mes.mm.model.StorageLocation;
import cn.b.sky.mes.mm.model.Warehouse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import cn.b.sky.mes.mm.model.condition.StorageLocationCondition;
import cn.b.sky.mes.mm.service.StorageLocationService;
import cn.b.sky.common.model.BatchJobReport;
import cn.b.sky.common.utils.DatabaseUtils;
import cn.b.sky.i18n.utils.LocaleResourceBundle;
import cn.b.sky.user.SecurityContextUtil;

@Service("storageLocationService")
public class StorageLocationServiceImpl implements StorageLocationService {

  private static final Logger LOG = LoggerFactory.getLogger(StorageLocationServiceImpl.class);

  @Resource
  StorageLocationDao storageLocationDao;

  @Override
  public List<StorageLocation> selectStorageLocationByCondition(StorageLocationCondition storageLocationCondition) {
    return storageLocationDao.selectStorageLocationByCondition(storageLocationCondition);
  }

  @Override
  public int selectStorageLocationCount(StorageLocationCondition storageLocationCondition) {
    return storageLocationDao.selectStorageLocationCount(storageLocationCondition);
  }

  @Override
  public StorageLocation selectStorageLocation(String siteCode, String storageLocationCode) {
    return storageLocationDao.selectStorageLocation(siteCode, storageLocationCode);
  }

  @Override
  public void insertStorageLocation(StorageLocation storageLocation) {
    storageLocationDao.insertStorageLocation(storageLocation);

  }

  @Override
  public void updateStorageLocation(StorageLocation storageLocation) {
    storageLocationDao.updateStorageLocation(storageLocation);

  }

  @Override
  public BatchJobReport deleteStorageLocation(String siteCode, String storageLocationCode) {
    String cause = "";
    Boolean success = true;
    try {
      storageLocationDao.deleteStorageLocation(siteCode, storageLocationCode);
    } catch (Exception e) {
      LOG.error("{}", e);
      TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
      if (DatabaseUtils.isConstraintsError(e.getMessage())) {
        // 外键删除已存在提示
        cause = LocaleResourceBundle.getCommonResourceLabel(SecurityContextUtil.getSiteCode(),SecurityContextUtil.getLanguageCode(),
            "dataBaseOperationPkFail.M")
            + LocaleResourceBundle.getCommonResourceLabel(SecurityContextUtil.getSiteCode(),SecurityContextUtil.getLanguageCode(), "notDelete.M");
      } else {
        cause = e.getMessage();
      }
      success = false;
    }
    return new BatchJobReport(storageLocationCode, cause, success, SecurityContextUtil.getLanguageCode(),SecurityContextUtil.getSiteCode());
  }

  @Override
  public List<Warehouse> selectWarehouseForStorageLocation(String siteCode) {
    return storageLocationDao.selectWarehouseForStorageLocation(siteCode);
  }

  @Override
  public List<StorageLocation> selectStorageLocationByWarehouse(String siteCode, String warehouseCode) {
    return storageLocationDao.selectStorageLocationByWarehouse(siteCode, warehouseCode);
  }

  @Override
  public List<StorageLocation> selectStorageLocationSuggests(StorageLocationCondition condition) {
    return storageLocationDao.selectStorageLocationSuggests(condition);
  }
}
